package com.xyd.servlet;

import com.xyd.entity.Comment;
import com.xyd.service.CommentService;
import com.xyd.service.impl.CommentServiceImpl;
import com.xyd.util.TotalPageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 任梦强
 */
@WebServlet("/CommentServlet")
public class CommentServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String currPage=request.getParameter("pageIndex");

        if (currPage==null){
            currPage="1";
        }
        int pageIndex=Integer.parseInt(currPage);
        CommentService commentService=new CommentServiceImpl();

        int count=commentService.findCountComment();
        TotalPageUtil totalPage=new TotalPageUtil();

        int totalPages= TotalPageUtil.getTotalPage(count,8);

            if (pageIndex<1){
               pageIndex=1;
        }
        if (pageIndex>totalPages){
            pageIndex=totalPages;
        }

        List<Comment> commentList=commentService.CommentByPage((pageIndex-1)*8,8);

        request.setAttribute("commentList",commentList);
        request.setAttribute("pageIndex",pageIndex);
        request.setAttribute("totalPages",totalPages);

        request.getRequestDispatcher("manage/guestbook.jsp").forward(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
